#include<iostream>
#include<map>
#include<string>
#include<utility>
using namespace std;

void test1(){
    map<int,string> myMap={{1,"set"},{2,"multiset"},{3,"map"},{4,"multimap"}};
    for(auto e: myMap){
        cout<<e.first<<" "<<e.second<<" ";
    }
    cout<<endl;
}

void test2(){
    map<int,string> myMap={{1,"set"},{2,"multiset"},{3,"map"},{4,"multimap"}};

    map<int,string>::iterator it1=myMap.begin();
    while(it1!=myMap.end()){
        cout<<(*it1).first<<" "<<(*it1).second<<" ";
        it1++;
    }
    cout<<endl;

    map<int,string>::iterator it2=myMap.begin();
    while(it2!=myMap.end()){
        cout<<it2->first<<" "<<it2->second<<" ";
        it2++;
    }
    cout<<endl;

    map<int,string>::reverse_iterator rit1=myMap.rbegin();
    while(rit1!=myMap.rend()){
        cout<<(*rit1).first<<" "<<(*rit1).second<<" ";
        rit1++;
    }
    cout<<endl;

    map<int,string>::reverse_iterator rit2=myMap.rbegin();
    while(rit2!=myMap.rend()){
        cout<<rit2->first<<" "<<rit2->second<<" ";
        rit2++;
    }
    cout<<endl;
}

void test3(){
    map<int,string> myMap={{1,"set"},{2,"multiset"},{3,"map"},{4,"multimap"}};
    pair<map<int,string>::iterator,bool> it1=myMap.insert(make_pair(3,"newmap"));
    cout<<it1.second<<" "<<it1.first->first<<" "<<it1.first->second<<endl;

    pair<map<int,string>::iterator,bool> it2=myMap.insert(make_pair(5,"string"));
    cout<<it2.second<<" "<<it2.first->first<<" "<<it2.first->second<<endl;

    for(const auto& e: myMap){
        cout<<e.first<<" "<<e.second<<" ";
    }
    cout<<endl;

}

void test4(){
    map<int,string> myMap={{1,"set"},{2,"multiset"},{3,"map"},{4,"multimap"}};
    auto it=myMap.find(3);
    myMap.erase(it);

    for(const auto& e: myMap){
        cout<<e.first<<" "<<e.second<<" ";
    }
    cout<<endl;

    auto it1=myMap.find(2);
    auto it2=myMap.find(4);
    myMap.erase(it1,it2);

    for(const auto& e: myMap){
        cout<<e.first<<" "<<e.second<<" ";
    }
    cout<<endl;
    
}

void test5(){
    map<int,string> myMap;
    myMap[1]="one";
    myMap[2]="two";
    myMap[3]="three";

    //访问元素
    cout<<myMap[2]<<endl;
    cout<<myMap[3]<<endl;

    //修改元素
    myMap[2]="map";
    cout<<myMap[2]<<endl;

    //尝试访问不存在的值，将插入新的键值队，键key是参数4，值value是默认初始化为空字符串
    cout<<myMap[4]<<endl;

    //遍历打印
    for(const auto& e: myMap){
        cout<<e.first<<" "<<e.second<<endl;
    }
    cout<<endl;
}

int main(){
    //test1();
    //test2();
    //test3();
    //test4();
    test5();

    return 0;
}